package cn.kmsoft.spatialboot.controller;

import cn.kmsoft.wololo.object.query.GeoJsonQuery;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.kmsoft.common.RestResponse;
import cn.kmsoft.wololo.object.query.AnalysisType;
import cn.kmsoft.common.exception.ExceptionMsg;
import cn.kmsoft.common.exception.ServiceException;
import cn.kmsoft.spatialboot.common.object.EllipsoidalQuery;
import cn.kmsoft.spatialboot.service.impl.EllipsoidalServiceImpl;
import cn.kmsoft.stgeometry.util.ShpUtil;
import cn.kmsoft.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;

/**
 * @author
 * @date 2019/7/26.
 */
@RestController
@Api(description = "辅助功能", tags = "3.Spatial Auxiliary")
@RequestMapping(produces = APPLICATION_JSON_VALUE)
public class SpatialAuxiliaryController {
    private static final Logger logger = LoggerFactory.getLogger(SpatialAuxiliaryController.class);
    private final EllipsoidalServiceImpl ellipsoidalService;

    @Autowired
    public SpatialAuxiliaryController(EllipsoidalServiceImpl ellipsoidalService) {
        this.ellipsoidalService = ellipsoidalService;
    }

    @ApiOperation(value = "计算椭球面积")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "椭球面积查询对象", paramType = "body", required = true, dataType = "EllipsoidalQuery")
    })
    @RequestMapping(value = "/calcEllipsoidalArea", method = RequestMethod.POST)
    public RestResponse calcEllipsoidalArea(@RequestBody EllipsoidalQuery query) throws IOException {
        if (query.getGeoJson().trim().startsWith("{")) {
            if (query.getDigits() == null) {
                query.setDigits(2);
            }

            return RestResponse.succeed(ellipsoidalService.calcEllipsoidalArea(query.getGeoJson(), query.getDigits()));
        }

        throw new ServiceException(ExceptionMsg.CLIENT_ERROR, "the format of geoJson is invalid");
    }

    @ApiOperation(value = "计算平面面积")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "平面面积查询对象", paramType = "body", required = true, dataType = "EllipsoidalQuery")
    })
    @RequestMapping(value = "/calcPlaneArea", method = RequestMethod.POST)
    public RestResponse calcPlaneArea(@RequestBody EllipsoidalQuery query) throws IOException {
        if (query.getGeoJson().trim().startsWith("{")) {
            if (query.getDigits() == null) {
                query.setDigits(4);
            }

            return RestResponse.succeed(ellipsoidalService.calcPlaneArea(query.getGeoJson(), query.getDigits()));
        }

        throw new ServiceException(ExceptionMsg.CLIENT_ERROR, "the format of geoJson is invalid");
    }

    @ApiOperation(value = "将shp转换为geojson格式，用于后续叠加分析。注意：该接口仅用于开发测试时使用，需要人工配置数据地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shpfilePath", value = "服务器上用于存放测试shp的文件夹物理目录", paramType = "query", required = true)
    })
    @RequestMapping(value = "/parseShp", method = RequestMethod.GET)
    public RestResponse shp2geojson(@RequestParam String shpfilePath) {
        //测试使用的默认shp文件夹地址
        if (StringUtil.isNullOrEmpty(shpfilePath)) {
            return RestResponse.failed(new Exception("shpfilePath is empty"));
        }

        GeoJsonQuery query = null;
        try {
            query = new GeoJsonQuery();
            JSONObject geojson = ShpUtil.parseShpByFile(shpfilePath);
            String jsonStr = JSON.toJSONString(geojson);

            query.setGeoJson(jsonStr);
            query.setDigits(4);
            query.setType(AnalysisType.INTERSECT);
            query.setReturnEllipsoidalArea(true);
            query.setWkid(4523);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return RestResponse.succeed(query);
    }

    @Test
    public void main() {
        String folderPath = "E:\\temp\\gtgis\\地块数据\\";
        GeoJsonQuery query = null;
        try {
            query = new GeoJsonQuery();
            JSONObject geojson = ShpUtil.parseShpByFolder(folderPath);
            String jsonStr = JSON.toJSONString(geojson);

            query.setGeoJson(jsonStr);
            query.setDigits(4);
            query.setType(AnalysisType.INTERSECT);
            query.setReturnEllipsoidalArea(true);
            query.setWkid(4523);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(JSON.toJSONString(query));
        RestResponse res = RestResponse.succeed(query);

        System.out.println(1);

    }
}
